#define GUILITE_ON  //Do not define this macro once more!!!
#include "GuiLite.h"
#include <stdlib.h>
#include <stdio.h>

#include "app_switch.h"
const int UI_WIDTH = 160;
const int UI_HEIGHT = 80;

static c_display* s_display;
static c_surface* s_surface;
static uint8_t change_main = 1;
static uint8_t selLists[5]={0};

extern const BITMAP_INFO LEDR_bmp,LEDG_bmp,LEDB_bmp,LEDP_bmp;

void change_focus(uint8_t change_main);
const char strlists[5][4]=
{
  "NC ",
  "GND",
  "SCK",
  "SDA",
  "VCC",
};

//////////////////////// define widgets & map message ////////////////////////
enum WND_ID
{
	ID_ROOT = 1,
  ID_MENU_UI1,
  ID_CTRL_UI2,
  ID_BUTTON_M1,
	ID_BUTTON_M2,
  ID_BUTTON_M3,
  ID_BUTTON_M4,
  ID_BUTTON_C5,
  ID_BUTTON_C1,
	ID_BUTTON_C2,
	ID_BUTTON_C3,
  ID_BUTTON_C4,
  ID_BMP1,
  ID_BMP2,
  ID_BMP3,
  ID_BMP4
};

class c_myUI : public c_wnd
{
  virtual void on_paint(void)
	{
		//c_rect rect;
		//get_screen_rect(rect);
		//m_surface->fill_rect(rect, GL_RGB(0, 0, 0), m_z_order);
	}

};
  
class c_ctrl_ui : public c_wnd
{
  void Button_sw_Init()
  {
    selLists[1] = 0;
    selLists[2] = 1;
    selLists[3] = 2;
    selLists[4] = 3;
    on_clicked(ID_BUTTON_C1,0);
    on_clicked(ID_BUTTON_C2,0);
    on_clicked(ID_BUTTON_C3,0);
    on_clicked(ID_BUTTON_C4,0);
  }
  
	virtual void on_init_children()
	{
		((c_button*)get_wnd_ptr(ID_BUTTON_C1))->set_on_click((WND_CALLBACK)&c_ctrl_ui::on_clicked);
		((c_button*)get_wnd_ptr(ID_BUTTON_C2))->set_on_click((WND_CALLBACK)&c_ctrl_ui::on_clicked);
		((c_button*)get_wnd_ptr(ID_BUTTON_C3))->set_on_click((WND_CALLBACK)&c_ctrl_ui::on_clicked);
    ((c_button*)get_wnd_ptr(ID_BUTTON_C4))->set_on_click((WND_CALLBACK)&c_ctrl_ui::on_clicked);
    ((c_button*)get_wnd_ptr(ID_BUTTON_C5))->set_on_click((WND_CALLBACK)&c_ctrl_ui::ponter_change_main);
	}
  
	virtual void on_paint(void)
	{
		//c_rect rect;
		//get_screen_rect(rect);
		//m_surface->fill_rect(rect, GL_RGB(0, 0, 0), m_z_order);
    
    c_button* button = (c_button*)get_wnd_ptr(ID_BUTTON_C5);
		button->show_window();
    
    Button_sw_Init();
	}
  
  void ponter_change_main(int ctrl_id, int param) {
    change_main = 1;
    change_focus(change_main);
  }
  
	void on_clicked(int ctrl_id, int param) {
    uint8_t pinmode;
    
		c_button* button = (c_button*)get_wnd_ptr(ctrl_id);
		switch (ctrl_id)
		{
      case ID_BUTTON_C1:
        selLists[0] = 1;
        break;
      case ID_BUTTON_C2:
        selLists[0] = 2;
        break;
      case ID_BUTTON_C3:
        selLists[0] = 3;
        break;
      case ID_BUTTON_C4:
        selLists[0] = 4;
        break;
		}
    
    if(selLists[selLists[0]] < 4)
      selLists[selLists[0]]++;
    else selLists[selLists[0]] = 0;
    
    button->set_str(strlists[selLists[selLists[0]]]);
		button->show_window();
    
    c_rect rect;
    get_screen_rect(rect);
    const BITMAP_INFO* bmp = NULL;
    switch (selLists[selLists[0]])
    {
      case 0:
        bmp = NULL;
        break;
      case 1:
        bmp = c_theme::get_bmp(BITMAP_CUSTOM3);
        break;
      case 2:
        bmp = c_theme::get_bmp(BITMAP_CUSTOM2);
        break;
      case 3:
        bmp = c_theme::get_bmp(BITMAP_CUSTOM4);
        break;
      case 4:
        bmp = c_theme::get_bmp(BITMAP_CUSTOM1);
        break;
      default:
        break;
    }
    if(bmp)
      c_bitmap::draw_bitmap(m_surface, m_z_order, bmp, 0, selLists[0]*20-19, GL_RGB(0, 0, 0));
    else s_surface->fill_rect(0, (selLists[0]-1)*20, 20, selLists[0]*20, GL_RGB(0, 0, 0), m_z_order);
  
    switch(selLists[selLists[0]])
    {
      case 0: pinmode = O_NON;break;
      case 1: pinmode = O_GND;break;
      case 2: pinmode = O_SCK;break;
      case 3: pinmode = O_SDA;break;
      case 4: pinmode = O_VCC;break;
    }
    
    switch (selLists[0])
		{
      case 1:
        sw.set.cfg.out1 = pinmode;
        break;
      case 2:
        sw.set.cfg.out2 = pinmode;
        break;
      case 3:
        sw.set.cfg.out3 = pinmode;
        break;
      case 4:
        sw.set.cfg.out4 = pinmode;
        break;
		}
  }
};

class c_menu_ui : public c_wnd
{
	virtual void on_init_children()
	{
    ((c_button*)get_wnd_ptr(ID_BUTTON_M1))->set_on_click((WND_CALLBACK)&c_menu_ui::ponter_change_stateui);
    ((c_button*)get_wnd_ptr(ID_BUTTON_M2))->set_on_click((WND_CALLBACK)&c_menu_ui::on_clicked);
    ((c_button*)get_wnd_ptr(ID_BUTTON_M3))->set_on_click((WND_CALLBACK)&c_menu_ui::on_clicked);
    ((c_button*)get_wnd_ptr(ID_BUTTON_M4))->set_on_click((WND_CALLBACK)&c_menu_ui::on_clicked);
	}
  
	virtual void on_paint(void)
	{
		c_rect rect;
		get_screen_rect(rect);
		m_surface->fill_rect(rect, GL_RGB(0, 0, 0), m_z_order);
    
	}
  
  void on_clicked(int ctrl_id, int param) {
    SW_Refresh();
  }
  
  void ponter_change_stateui(int ctrl_id, int param) {
    change_main = 0;
    change_focus(change_main);
  }
  
  
};






//////////////////////// layout UI ////////////////////////
static c_myUI s_myUI;
static c_ctrl_ui s_ctrl_ui;
static c_menu_ui s_menu_ui;

static c_label s_label1, s_label2, s_label3,s_label4;
static c_button s_button_c1, s_button_c2, s_button_c3, s_button_c4,s_button_c5; 
static c_button s_button_m1, s_button_m2, s_button_m3, s_button_m4;
static c_list_box s_list_box;

static WND_TREE s_ctrl_widgets[] =
{
  {&s_button_c5, ID_BUTTON_C5, "EXIT",60, 30, 90, 50, NULL},
	{&s_button_c1, ID_BUTTON_C1, "NC ", 18, 0,  40, 20, NULL},
	{&s_button_c2, ID_BUTTON_C2, "NC ", 18, 20, 40, 20, NULL},
	{&s_button_c3, ID_BUTTON_C3, "NC ", 18, 40, 40, 20, NULL},
  {&s_button_c4, ID_BUTTON_C4, "NC ", 18, 60, 40, 20, NULL},
	{ NULL,0,0,0,0,0,0 }
};

static WND_TREE s_menu_widgets[] ={
  {&s_button_m1, ID_BUTTON_M1, "设置",   0,  30, 40, 25, NULL},
  {&s_button_m2, ID_BUTTON_M2, " 开关:", 50, 30, 40, 25, NULL},
  {&s_button_m3, ID_BUTTON_M3, "切换",   0, 55, 40, 25, NULL},
  {&s_button_m4, ID_BUTTON_M4, "模式",   50, 55, 40, 25, NULL},
  
  { NULL,0,0,0,0,0,0 }
};

static WND_TREE s_myUI_children[] =
{
  {&s_ctrl_ui,ID_CTRL_UI2,0,0, 0,60,80,s_ctrl_widgets},
  {&s_menu_ui,ID_MENU_UI1,0,60,0,100,80,s_menu_widgets},
  
  
	{ NULL,0,0,0,0,0,0 }
};
 //{&s_list_box,	ID_LIST_BOX,"listBox",	100, 0, 40, 20},
//////////////////////// start UI ////////////////////////
extern const LATTICE_FONT_INFO MiSans_Demibold_20B;  

void load_resource()
{
  c_theme::add_bitmap(BITMAP_CUSTOM1,&LEDR_bmp);
  c_theme::add_bitmap(BITMAP_CUSTOM2,&LEDG_bmp);
  c_theme::add_bitmap(BITMAP_CUSTOM3,&LEDB_bmp);
  c_theme::add_bitmap(BITMAP_CUSTOM4,&LEDP_bmp);
	c_theme::add_font(FONT_DEFAULT, &MiSans_Demibold_20B);//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789(_+=./-:;@%!#',?)<>▲▼正反转转速确定切换设置模式取消关闭
	//for button
	c_theme::add_color(COLOR_WND_FONT,   GL_RGB(255, 255, 243));
	c_theme::add_color(COLOR_WND_NORMAL, GL_RGB(0, 0, 0));        
	c_theme::add_color(COLOR_WND_PUSHED, GL_RGB(33, 42, 53));
	c_theme::add_color(COLOR_WND_FOCUS,  GL_RGB(78, 198, 76));   
	c_theme::add_color(COLOR_WND_BORDER, GL_RGB(46, 59, 73));
}

void create_ui(void* phy_fb, int screen_width, int screen_height, int color_bytes)
{
	load_resource();

	static c_surface surface(UI_WIDTH, UI_HEIGHT, color_bytes, Z_ORDER_LEVEL_0);
	static c_display display(phy_fb, screen_width, screen_height, &surface);
	s_surface = &surface;
	s_display = &display;
	
	s_myUI.set_surface(s_surface);
	s_myUI.connect(NULL, ID_ROOT, 0, 0, 0, UI_WIDTH, UI_HEIGHT, s_myUI_children);
  
  
	s_myUI.show_window();
}

void change_focus(uint8_t change_main)
{
   if(change_main)
    s_myUI.set_child_focus(&s_menu_ui);
  else 
    s_myUI.set_child_focus(&s_ctrl_ui);
}

//////////////////////// interface for all platform ////////////////////////
extern "C" void startHelloNoTouch(void* phy_fb, int width, int height, int color_bytes)
{
	create_ui(phy_fb, width, height, color_bytes);
}

//模拟指针电机
extern "C" void sendTouch2HelloNoTouch(int x, int y, bool is_down)
{
	is_down ? s_myUI.on_touch(x, y, TOUCH_DOWN) : s_myUI.on_touch(x, y, TOUCH_UP);
}

//模拟按键
extern "C" void sendKey2HelloNoTouch(unsigned int key)
{
  if(change_main)
    s_menu_ui.on_navigate(NAVIGATION_KEY(key));
  else 
    s_ctrl_ui.on_navigate(NAVIGATION_KEY(key));
}

extern void* getUiOfHelloNoTouch(int* width, int* height, bool force_update = false)
{
	if (s_display)
	{
		return s_display->get_updated_fb(width, height, force_update);
	}
	return NULL;
}

int captureUiOfHelloNoTouch()
{
	return s_display->snap_shot("snap_short.bmp");
}
